<?php
    session_start();
    /*if(!isset($_SESSION['usuario'])){
            header('location:../html/login.html');
         }*/
         
    $cdoi = $_REQUEST['coddi'];
    $code = $_REQUEST['codde'];
    
    mssql_connect("192.168.10.9\DINAMICA", "sa", "admin4");
    mssql_select_db("DGempres10");
    $oidi= "SELECT OID FROM  DGempres10..GENDIAGNO 
            WHERE DIACODIGO = '".$cdoi."'";
    $lsti = mssql_query($oidi);
    
    while ($lsti2 = mssql_fetch_array($lsti)){
        $oi = $lsti2['OID'];
    }
    
    
    $oide= "SELECT OID FROM  DGempres10..GENDIAGNO 
            WHERE DIACODIGO = '".$code."'";
    $lste = mssql_query($oide);
    
    while ($lste2 = mssql_fetch_array($lste)){
        $oe =$lste2['OID'];
    }
    
    mssql_close();
    
    
    //$documento = $_REQUEST['txtDocP'];
    //echo$nombre = $_REQUEST['txtNombres'];
    //echo$apellidos = $_REQUEST['txtApellidos'];
    //echo$fechanac = $_REQUEST['txtFechaNac'];
    //echo$edad = $_REQUEST['txtEdad'];
    $ingresos = $_REQUEST['cbIngresos'];
    //echo$fecha = $_REQUEST['txtFecha'];
    //echo$asegurador = $_REQUEST['txtAsegurador'];
    //$diagnosticoi = $_REQUEST['diagIngreso'];
    $procedencia = $_REQUEST['cbProcedencia'];
    $conducta = $_REQUEST['cbConducta'];
   /* $estado = $_REQUEST['cbEstadoA'];
    $ftp = $_REQUEST['formaTermP'];
    $fechap = $_REQUEST['txtFechaP'];
    $hora = $_REQUEST['hora'];
    $minuto = $_REQUEST['minutos'];
    $analgesia = $_REQUEST['cbAnalgO'];
    $cauna = $_REQUEST['caunanalgesia'];
    //$diagnosticoe = $_REQUEST['diagEgreso'];
    $medico = $_REQUEST['medico'];  */
    $oid = $_REQUEST['oid'];
    //$horap = $hora.":".$minuto;
    //$ccesarea = $_REQUEST['cbCc'];
    
    $di = $_REQUEST['txtDi'];
    $de = $_REQUEST['txtDe'];
    if($de == null){
        $de2 = 10;
    }else{
        $de2 = $_REQUEST['txtDe'];
    }
    
    
    
    include_once '../../../../controlador/php/conexion/Conexion.php';
    $con = new Conexion();
    
    /*$val = "select COUNT(*) from atenMaterna
            where genPacien = ".$oid." and adnIngreso = ".$ingresos;
    $count = $con->query($val);
    $co = mssql_result($count, 0, 0);
    
    if($co === 1){
        header("location:../../../vista/php/error.php");
    }else{*/
    $count =  "select COUNT(*) as n from atenMaterna
               where genPacien =".$oid." and ate_diagEgreso =  10";
    $res  = $con ->query($count);
    
    while($res2 = mssql_fetch_array($res)){
        $n = $res2['n'];
    }
    if ($n > 0){
        header("location:../../../vista/php/error.php");
    }  else {
        
    $sql = "insert into atenMaterna(ate_fecha,genPacien,adnIngreso,ate_procede,ate_conducta,ate_estadoA,ate_formTerEmb,ate_ccesarea,ate_fechaPar,ate_horaPar,ate_analgesia,ate_caunAnalg,genMedico,ate_diagIngreso,ate_diagEgreso,diagIngreso,diagEgreso,usu_oid)
            values(GETDATE(),".$oid.",".$ingresos.",'".$procedencia."','".$conducta."','".$estado."','".$ftp."','".$ccesarea."','".$fechap."','".$horap."','".$analgesia."','".$cauna."','".$medico."','".$di."','".$de2."','".$oi."','".$oe."',".$_SESSION['usuario'].")";
    $con->query($sql) or die("error");
    
    $oidat = "select ate_oid from atenMaterna
            where genPacien =".$oid;
    $oidate = $con->query($oidat) or die("error");
    
    while ($res = mssql_fetch_array($oidate)){
        $oida = $res['ate_oid'];
    }
    
    $sexo = $_POST['sexo'];
    $peso = $_POST['peso'];
    $talla = $_POST['talla'];
    $certificado = $_POST['certificado'];
    
    $n  = count($sexo);
    $i = 0;
    
    while ($i < $n)
    {
       $sql2 = "insert into hijoMater(ate_oid,hij_sexo,hij_peso,hij_talla,hij_certificado)values(".$oida.",'{$sexo[$i]}',{$peso[$i]},{$talla[$i]},{$certificado[$i]})";
       $con->query($sql2) or die("error");
       $i++;
    }
    header("location:../../../vista/php/exitoSeg.php");
    }
    $con->cerrarConexion();
    //}
?>
